<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #myCanvas {
      display: inherit;
      margin: 50px auto 0;
      border: 1px solid #000;
    }
  </style>
</head>

<body>
  <canvas id="myCanvas" width="800" height="500"></canvas>

  <script>
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');
    const canvasWidth = canvas.width;
    const canvasHeight = canvas.height;
    console.log('Canvas宽度：', canvasWidth);
    console.log('Canvas高度：', canvasHeight);

    const devicePixelRatio = window.devicePixelRatio || 1;// 2
    console.log(29, 'devicePixelRatio', devicePixelRatio);

    // 设置canvas的CSS宽度和高度
    canvas.style.width = canvas.width + 'px';
    canvas.style.height = canvas.height + 'px';

    // 设置canvas的物理宽度和高度
    canvas.width = canvas.width * devicePixelRatio;
    canvas.height = canvas.height * devicePixelRatio;

    // 缩放绘图上下文，使其与物理像素一致
    ctx.scale(devicePixelRatio, devicePixelRatio);// 防止线条颜色失真
    // ctx.scale(0.5, 0.5);// 缩放一倍

    ctx.strokeStyle = 'rgba(0, 0, 0, 1)';
    ctx.lineWidth = 1; // 线条宽度

    // 绘制表格的行
    for (var i = 0; i < 17; i++) {
      ctx.beginPath();
      ctx.moveTo(0, (i + 1) * 30); // 行的起始点
      ctx.lineTo(canvasWidth, (i + 1) * 30); // 行的结束点
      ctx.stroke(); // 绘制行
    }

    // 绘制表格的列
    for (var j = 0; j < 16; j++) {
      ctx.beginPath();
      ctx.moveTo((j + 1) * 50, 0); // 列的起始点
      ctx.lineTo((j + 1) * 50, canvasHeight); // 列的结束点
      ctx.stroke(); // 绘制列
    }
  </script>
</body>

</html>